<?php
/**
 * Created by PhpStorm.
 * 
 */

class ModAchievement extends Model {

    protected $table = 'achievement';

    // 表名
    public function tableName(){
        return $this->table;
    }

    public static function model($className=__CLASS__){
        return parent::model($className);
    }

    public function getDbConnection(){
        return Yii::app()->dbOss;
    }

    public function addmuch($params){
    	$result = $this->getDbConnection()->multiInsert($params, $this->table, FALSE);
    	return $result;
    }
    
    public function select($params){
    	$table = $this->table;
    	$where  = ' WHERE game=:game ';
    	$args   = array(
    			':game'     => $params['g']
    	);
    	 
    	if(!empty($params['a_id'])){
    		$where .= " AND a_id like :a_id ";
    		$args['a_id'] = '%'.$params['a_id'].'%';
    	}
    
    	if(!empty($params['achievement_name'])){
    		$where .= " AND achievement_name like :achievement_name ";
    		$args['achievement_name'] = '%'.$params['achievement_name'].'%';
    	}    	 

    	$fields = "id,achievement_name,type_name,subtype_name,achievement_desc ";
    	$sql = "SELECT {$fields} FROM {$table} {$where}";
    	$data = $this->getDbConnection()->query($sql, $args);    	
    	return $data;
    }
    
    public function page($params){
    	$table = $this->table;
    	$where  = ' WHERE game=:game ';
    	$args   = array(
    			':game'     => $params['g']
    	);
    	
        if(!empty($params['a_id'])){
            $where .= " AND a_id=:a_id ";
            $args['a_id'] = $params['a_id'];
        }
        
        if(!empty($params['achievement_name'])){
        	$where .= " AND achievement_name like :achievement_name ";
        	$args['achievement_name'] = '%'.$params['achievement_name'].'%';
        }
    	
    	$c_sql = "select count(1) as cnt from {$table} {$where}";
    	$c_result = $this->getDbConnection()->query($c_sql, $args);
    	$count = isset($c_result[0]['cnt']) ? $c_result[0]['cnt'] : 0;

    	$order  = self::getOrder($params['order']);
    	$limit  = PubFunc::getLimit($params['length'], $params['page']);
    	$fields = "id,a_id,achievement_name,type_name,subtype_name,achievement_desc";
    	$sql = "SELECT {$fields} FROM {$table} {$where} {$order} {$limit}";
    	$data = $this->getDbConnection()->query($sql, $args);
    	$result = array('count'=>$count,'data'=>$data);
    	return $result;
    }
    
    public static function getOrder($order){
    	if(PubFunc::checkEmpty($order, array(0, 1))) return '';
    	if(!in_array($order[1], array('asc', 'desc'))){
    		$sort = 'asc';
    	}else{
    		$sort = $order[1];
    	}
    	return " ORDER BY {$order[0]} {$sort} ";
    }
    
    public function getGid(){
    	$sql = "SELECT a_id FROM {$this->table}";
    	$result = $this->getDbConnection()->getAll($sql, $args);
    	return $result;
    }
    
    public function getInfo($g_id_array){
    	if(count($g_id_array) == 0){
    		return array();
    	}
    	$g_ids = implode(',', $g_id_array);    	
    	$sql = "SELECT a_id,achievement_name,achievement_desc FROM {$this->table} WHERE a_id IN ({$g_ids})";
    	$result = $this->getDbConnection()->getAll($sql, $args);
    	return $result;
    }
    

    
    
} 